package com.rico.deploy.controller;

import com.github.pagehelper.PageInfo;
import com.rico.common.response.MessageUtils;
import com.rico.common.response.RestResponse;
import com.rico.deploy.entity.Academic;
import com.rico.deploy.entity.Campus;
import com.rico.deploy.service.CampusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 吴泽胜
 * @FileName CampusController
 * @date 2021-05-05 6:56 下午
 * @Software: IntelliJ IDEA
 */
@Controller
@Slf4j
public class CampusController {

    @Autowired
    private CampusService campusService;

    @GetMapping("campus/list")
    @ResponseBody
    public RestResponse getlist(Integer page, Integer limit) {
        List<Campus> campuses = campusService.selectAll(page, limit);
        PageInfo<Academic> campusPageInfo = new PageInfo(campuses);
        return MessageUtils.success(campusPageInfo.getList(), (int) campusPageInfo.getTotal());
    }


    @GetMapping("campus/lists")
    @ResponseBody
    public RestResponse getlist() {
        List<Campus> campuses = campusService.selectAll();
        return MessageUtils.success(campuses);
    }


    @GetMapping("campus/delete")
    @ResponseBody
    public RestResponse deleteById(Integer id) {
        log.info("{}", id);
        String msg = null;
        if ( campusService.deleteCampusById(id) == 1 )
            msg = "删除成功";
        else
            msg = "删除失败";
        return MessageUtils.success(msg);
    }


    @PostMapping("campus/put")
    @ResponseBody
    public RestResponse put(Campus campus) {


        int i = campusService.updateCampus(campus);
        if ( i == 0 )
            return MessageUtils.fail("编辑失败");

        else
            return MessageUtils.success("编辑成功");
    }

    @PostMapping("campus/post")
    @ResponseBody
    public RestResponse post(Campus campus) {

        int i = campusService.insertCampus(campus);
        if ( i == 0 )
            return MessageUtils.fail("添加失败");

        else
            return MessageUtils.success("添加成功");
    }

    /**
     * 获取html页面
     * @return
     */
    @GetMapping("campusManage")
    public String gethtml(){
        return "page/campusManage";
    }


}
